package v1

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"strconv"
	"time"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/admin/middleware"
	"zimuzu_cvn_web_api/service"
)

type YtaAPI struct {
}

func (yta YtaAPI) Name() string {
	return "yta API"
}

func (yta YtaAPI) Bind() {
	ytaAPI := config.C.Router.Group("/v1/admin/yta", middleware.TrackLog(), middleware.AuthMiddleware())
	{
		ytaAPI.GET("recharge/set", rechargeSet)
		ytaAPI.POST("recharge/set/batch", rechargeBatchSet)
		ytaAPI.GET("recharge/list", rechargeList)
		ytaAPI.GET("address/list", ytaAddressList)
		ytaAPI.GET("address/set", ytaAddressSet)
		ytaAPI.POST("address/set/batch", ytaAddressBatchSet)
		ytaAPI.GET("address/down", ytaAddressDown)
		ytaAPI.GET("recharge/down", ytaRechargeDown)
	}
}

// 批量设置YTA地址
func ytaAddressBatchSet(c *gin.Context) {

	var params model.RechargeBatchSetParams
	err := c.BindJSON(&params)
	e.Log("param", params)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}

	// 调用单节点处理
	errDo := 0
	for _, k := range params.IDS {
		err = model.UpdateYtaAddressStatus(k, params.Status)
		if err != nil {
			errDo++
		}
	}

	if errDo > 0 {
		errno.PledgeStatusBatchErr.Info += "失败" + strconv.Itoa(errDo) + "条"
		api.SendResponse(c, errno.YtaStatusBatchErr, nil)
		return
	} else {
		e.Log("info", "没有数据需要处理")
	}

	errno.OK.Info = "批量处理成功"
	api.SendResponse(c, errno.OK, nil)
	return
}

// 批量审核状态
func rechargeBatchSet(c *gin.Context) {

	var params model.RechargeBatchSetParams
	err := c.BindJSON(&params)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}

	// 调用单节点处理
	errDo := 0
	for _, k := range params.IDS {
		err = service.SingleUpdateAdminYtaRecharge(k, params.Status)
		if err != nil {
			errDo++
		}
	}

	if errDo > 0 {
		errno.PledgeStatusBatchErr.Info += "失败" + strconv.Itoa(errDo) + "条"
		api.SendResponse(c, errno.YtaStatusBatchErr, nil)
		return
	}

	errno.OK.Info = "批量处理成功"
	api.SendResponse(c, errno.OK, nil)
	return
}

// 单个审核状态
func rechargeSet(c *gin.Context) {

	var params model.RechargeSetParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}

	err = service.SingleUpdateAdminYtaRecharge(params.ID, params.Status)
	if err != nil {
		e.Log("info", err.Error())
		api.SendResponse(c, errors.New("设置失败"), nil)
		return
	}

	errno.OK.Info = "设置成功"
	api.SendResponse(c, errno.OK, nil)
	return
}

func ytaAddressSet(c *gin.Context) {

	var params model.RechargeSetParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}

	err = model.UpdateYtaAddressStatus(params.ID, params.Status)
	if err != nil {
		api.SendResponse(c, errors.New("设置失败"), nil)
		return
	}

	errno.OK.Info = "设置成功"
	api.SendResponse(c, errno.OK, nil)
	return
}

func rechargeList(c *gin.Context) {

	var params model.RechargeListParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	if params.Page < 1 {
		params.Page = 1
		params.Per = 20
	}
	if params.Start == "" {
		params.Start = "2020-01-01 00:00:00"
	}
	if params.End == "" {
		params.End = time.Now().Format(e.TimeFormatS)
	}
	res, err := service.GetAdminYtaRechargeLog(params.TargetUid, params.Page, params.Per, params.Status, params.Start, params.End)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "获取数据成功"
	api.SendResponse(c, errno.OK, res)
	return
}

func ytaAddressList(c *gin.Context) {

	var params model.RechargeListParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	if params.Page < 1 {
		params.Page = 1
		params.Per = 20
	}
	if params.Start == "" {
		params.Start = "2020-01-01 00:00:00"
	}
	if params.End == "" {
		params.End = time.Now().Format(e.TimeFormatS)
	}
	res, err := service.GetAdminYtaAddressList(params.TargetUid, params.Page, params.Per, params.Status, params.Start, params.End)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "获取数据成功"
	api.SendResponse(c, errno.OK, res)
	return
}

// 地址导出
func ytaAddressDown(c *gin.Context) {

	var params model.RechargeListDownParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	if params.Start == "" {
		params.Start = "2020-01-01 00:00:00"
	}
	if params.End == "" {
		params.End = time.Now().Format(e.TimeFormatS)
	}

	downUrl, err := service.DownExcelWithYtaAddressList(params.TargetUid, params.Status, params.Start, params.End)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	url := config.C.Domains.ResourceExcel + downUrl
	errno.OK.Info = "获取导出数据成功"
	api.SendResponse(c, errno.OK, url)
	return
}

// 兑换记录导出
func ytaRechargeDown(c *gin.Context) {

	var params model.RechargeListDownParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	if params.Start == "" {
		params.Start = "2020-01-01 00:00:00"
	}
	if params.End == "" {
		params.End = time.Now().Format(e.TimeFormatS)
	}

	downUrl, err := service.DownExcelWithYtaRechargeList(params.TargetUid, params.Status, params.Start, params.End)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	url := config.C.Domains.ResourceExcel + downUrl
	errno.OK.Info = "获取导出数据成功"
	api.SendResponse(c, errno.OK, url)
	return
}
